﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using com.lover.astd.common.model;
using com.lover.astd.common.config;
using System.Windows.Forms;
using System.Collections;
using com.lover.astd.common;
using com.lover.astd.common.logicexe.hero;

namespace com.lover.astd.game.ui.server.impl.troop
{
    /// <summary>
    /// 英雄突飞服务
    /// </summary>
    public class HeroTrainServer : LogicServer
    {
        public HeroTrainServer(MainForm frm) { _mainForm = frm; ServerName = ConfigStrings.S_HeroTrain; ServerReadableName = ConfigStrings.SR_HeroTrain; }
        public override void saveSettings()
        {
            GameConfig conf = getConfig();
            MainForm frm = _mainForm;
            conf.setConfig(ServerName, ConfigStrings.enabled, frm.chk_hero_tufei_enable.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.nocd, frm.chk_hero_nocd.Checked.ToString());
            conf.setConfig(ServerName, ConfigStrings.giftevent, frm.chk_hero_giftevent.Checked.ToString());
            // search dg_heros for who are selected
            string selectedHeroIds = getDataGridSelectedIds<Hero>(frm.dg_heros);
            conf.setConfig(ServerName, ConfigStrings.train_heros, selectedHeroIds);
        }
        public override void renderSettings()
        {
            if (_mainForm.dg_heros.DataSource == null)
            {
                BindingSource src = new BindingSource();
                src.DataSource = getUser().Heroes;
                _mainForm.dg_heros.AutoGenerateColumns = false;
                _mainForm.dg_heros.DataSource = src;
            }

            Dictionary<string, string> conf = getConfig(ServerName);
            if (conf.Count == 0) return;
            MainForm frm = _mainForm;
            frm.chk_hero_tufei_enable.Checked = conf.ContainsKey(ConfigStrings.enabled) && conf[ConfigStrings.enabled].ToLower().Equals("true");
            frm.chk_hero_nocd.Checked = conf.ContainsKey(ConfigStrings.nocd) && conf[ConfigStrings.nocd].ToLower().Equals("true");
            frm.chk_hero_giftevent.Checked = conf.ContainsKey(ConfigStrings.giftevent) && conf[ConfigStrings.giftevent].ToLower().Equals("true");
            if (conf.ContainsKey(ConfigStrings.train_heros) && conf[ConfigStrings.train_heros] != "")
            {
                string train_heros = conf[ConfigStrings.train_heros];
                renderDataGridToSelected<Hero>(frm.dg_heros, train_heros);
            }
        }
        public override void loadDefaultSettings()
        {
            GameConfig conf = getConfig();
            conf.setConfig(ServerName, ConfigStrings.enabled, "true"); // 突飞开关
            conf.setConfig(ServerName, ConfigStrings.giftevent, "true"); //秒CD
            conf.setConfig(ServerName, ConfigStrings.nocd, "false"); //秒CD
            conf.setConfig(ServerName, ConfigStrings.train_heros, ""); // 突飞的英雄, 包含了,分割的英雄id
            renderSettings();
        }
    }
}
